<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#assign currentMenu="strategyDetail"/>
    <#include "../common/header.ftl">
    <script src="/js/plugins/My97DatePicker/WdatePicker.js"></script>

</head>
<body>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-lg-3">
            <#include "../common/menu.ftl">
        </div>
        <div class="col-lg-9">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-head-line">攻略文章管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="/strategyDetail/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <div class="form-group">
                    <label>关键字:</label>
                    <input type="text" class="form-control" name="keyword" value="${(qo.keyword)!}"
                           placeholder="请输入标题/攻略分类">
                </div>
                <button class="btn btn-info">
                    <span class="glyphicon glyphicon-search"></span> 查询
                </button>
                <a href="javascript:" class="btn btn-success inputBtn">
                    <span class="glyphicon glyphicon-plus"></span> 新增
                </a>
            </form>

            <table class="table table-striped table-hover">
                <tr>
                    <th>序号</th>
                    <th>标题</th>
                    <th>封面</th>
                    <th>发布时间</th>
                    <th>排序</th>
                    <th>攻略类别</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
                <#list pageInfo.list as entity>
                    <tr>
                        <td>${entity_index + 1}</td>
                        <td>${entity.title}</td>
                        <td><img src="${entity.coverUrl}" width="100"/></td>
                        <td>${(entity.releaseTime?string('yyyy-MM-dd HH:mm:ss'))!}</td>
                        <td>${entity.sequence}</td>
                        <td>${entity.strategyCatalog.name}</td>
                        <td>${entity.stateName}</td>
                        <td>
                            <a href="javascript:" class="btn btn-primary btn-xs inputBtn" data-json='${entity.json}'>
                                <span class="glyphicon glyphicon-pencil"></span> 修改
                            </a>
                        </td>
                    </tr>
                </#list>
            </table>
            <#include "../common/page.ftl">
        </div>
    </div>
</div>

<#--推荐攻略模态框-->
<div id="inputModal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/strategyDetail/saveOrUpdate.do" method="post"
                      enctype="multipart/form-data" id="editForm">
                    <input type="hidden" name="id"/>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">标题：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="title"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">封面：</label>
                        <div class="col-lg-6">
                            <img id="coverUrl" style="width: 100%"/>
                            <input type="file" class="form-control" name="img"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">所属攻略：</label>
                        <div class="col-lg-6">
                            <select class="form-control" id="strategy">
                                <#list strategies as s>
                                    <option value="${s.id}">${s.title}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">攻略类别：</label>
                        <div class="col-lg-6">
                            <select class="form-control" name="strategyCatalog.id">
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">排序：</label>
                        <div class="col-lg-6">
                            <input type="number" class="form-control" name="sequence"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">状态：</label>
                        <div class="col-lg-6">
                            <select class="form-control" name="state">
                                <option value="0">发布</option>
                                <option value="1">草稿</option>
                            </select>
                        </div>
                    </div>
                <#--ck富文本编辑器容器-->
                    <div class="form-group">
                        <textarea name="strategyContent.content" id="ckeditor"></textarea>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary submitBtn">保存</button>
            </div>
        </div>
    </div>
</div>
<script src="/js/ckeditor/ckeditor.js"></script>
<script type="text/javascript">

    // 页面加载完毕
    $(function () {
        // 初始化 ckeditor 编辑器
        var ckeditor = CKEDITOR.replace('ckeditor');

        // 给所有的新增, 修改按钮绑定点击事件
        $(".inputBtn").click(function () {
            // 弹出模态框先清空内容
            $("#editForm input").val("");
            $("#coverUrl").attr("src", "");

            // 清空ckeditor 富文本框中的内容
           // ckeditor.setData("");

            // 若是修改按钮点击事件, 则需回显数据
            var json = $(this).data("json");
            if (json) {
                // 数据渲染
                $("#editForm input[name='id']").val(json.id);
                $("#editForm input[name='title']").val(json.title);
                $("#coverUrl").attr("src", json.coverUrl);
                $("#editForm input[name='sequence']").val(json.sequence);
                $("#editForm select[name='state']").val(json.state);
                $("#editForm select[id='strategy']").val(json.strategyId);

                // 发送异步请求查询
                // 调用二级联动函数获取大攻略对应的攻略类别
                linkage(json.strategyId);

                setTimeout(function () {
                    $("#editForm select[name='strategyCatalog.id']").val(json.strategyCatalog.id);
                },100)
                // 发送异步请求获取攻略文章内容
                $.get("/strategyDetail/getContent.do", {id: json.id}, function (data) {
                    ckeditor.setData(data.content);
                });
            } else {
                // 弹出模态框之前, 需要先查询第一个攻略的攻略类别
                // 新增, 获取所属攻略下拉框中的第一个攻略值
                var strategyId = $("#strategy option:first").val();
                // 调用二级联动函数获取大攻略对应的攻略类别
                linkage(strategyId);
            }
            $("#inputModal").modal("show");
        });

        // 获取指定大攻略所属的所有攻略类别, 并拼接到下拉框中
        function linkage(strategyId) {
            // 发送ajax请求获取攻略类别
            $.get("/strategyCatalog/getCatalogsByStrategyId.do",
                    {strategyId: strategyId}, function (data) {
                        // 每次拼接选项之前先清空下拉框的选项
                        $("#editForm select[name='strategyCatalog.id']").html("");
                        // 遍历攻略类别数组
                        $.each(data, function (index, ele) {
                            // 将得到的攻略类别数据拼接到攻略类型下拉框中
                            $("<option></option>").html(ele.name).val(ele.id)
                                    .appendTo("#editForm select[name='strategyCatalog.id']");

                        });
                    })
        }

        // 给选择大攻略下拉框绑定值改变事件
        $("#strategy").change(function () {
            // 调用二级联动函数获取大攻略对应的攻略类别, 并传入下拉框选中的值
            linkage($("#strategy").val());
        });

        // 给模态框保存按钮绑定点击事件
        $(".submitBtn").click(function () {
            // 提交表单之前先获取ck编辑器中的文本内容保存到id为"ckeditor"的超文本框中
            $("#ckeditor").html(ckeditor.getData());
            // 提交表单
            $("#editForm").submit();
        });
    });


</script>
</body>
</html>